﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VideoRentalShop.Controller;
using VideoRentalShop.Model;

namespace VideoRentalShop.View.Windows.MovieList
{
    /// <summary>
    /// Logika interakcji dla klasy SearchPanel.xaml
    /// </summary>
    public partial class SearchPanel : UserControl
    {
        private VideoRentalShopController _controller = VideoRentalShopController.Instance;


        public SearchPanel()
        {
            InitializeComponent();
            
            foreach (var genre in _controller.Data.Genre.GetAll().OrderBy(x => x.Title))
            {
                lstGenre.Items.Add(new CheckBox { Content = genre.Title, Tag = genre.Id });
            }
        }


        public void Clear()
        {
            txtTitle.Text = string.Empty;
            txtDirector.Text = string.Empty;
            optAge0.IsChecked = true;
            txtCountry.Text = string.Empty;
            dtpReleaseDate.Value = null;

            foreach (var genre in lstGenre.Items)
            {
                var chb = genre as CheckBox;
                chb.IsChecked = false;
            }
        }

        public IEnumerable<Movie> GetResults()
        {
            var title = txtTitle.Text.Trim().ToLower();
            var director = txtDirector.Text.Trim().ToLower();
            var country = txtCountry.Text.Trim().ToLower();
            int? releaseYear = dtpReleaseDate.Value.HasValue ? dtpReleaseDate.Value.Value.Year : (int?)null;
            var minAge = 0;
            if (optAge1.IsChecked == true) { minAge = 7; }
            else if (optAge2.IsChecked == true) { minAge = 12; }
            else if (optAge3.IsChecked == true) { minAge = 16; }
            else if (optAge4.IsChecked == true) { minAge = 18; }


            var results = _controller.Data.Movie.GetAll().Where(x => x.MinAge >= minAge);

            if (!string.IsNullOrWhiteSpace(title))
            {
                results = results.Where(x => x.Title.ToLower().Contains(title) || x.OriginalTitle.ToLower().Contains(title));
            }
            if (!string.IsNullOrWhiteSpace(director))
            {
                results = results.Where(x => x.Director.ToLower().Contains(director));
            }
            if (!string.IsNullOrWhiteSpace(country))
            {
                results = results.Where(x => x.OriginCountry.ToLower().Contains(country));
            }
            if (releaseYear.HasValue)
            {
                results = results.Where(x => x.ReleaseDate.HasValue && x.ReleaseDate.Value.Year == releaseYear.Value);
            }

            foreach (var genre in lstGenre.Items)
            {
                var chb = genre as CheckBox;
                if (chb.IsChecked == true)
                {
                    var id = (long)chb.Tag;
                    var avaiableIds = _controller.Data.GenreDetails.GetAll().Where(x => x.GenreId == id).Select(x => x.MovieId);
                    results = results.Where(x => avaiableIds.Contains(x.Id));
                }
            }

            return results;
        }
    }
}
